﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public partial class XSSJ_XSSJQLMX : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      if (!ProjectPrivate.GetQX("6"))
      {
        Server.Transfer("~\\blank.aspx");
      }
      if (CWSD.GetSDZT("是"))
      {
        Server.Transfer("~\\sd.aspx");
      }
      string sID = CommonUse.GetID("id");
      if (!string.IsNullOrEmpty(sID))
        InitData(sID);
      string sMode = CommonUse.GetID("mode");
      ASPxHyperLink1.NavigateUrl = "QLMXBack.aspx?id=" + sID + "&mode=" + sMode;
      //if (sMode == "1")
      //{
      //  ASPxHyperLink1.NavigateUrl = "XSSJKHList.aspx";
      //}
      //else
      //{
      //  ASPxHyperLink1.NavigateUrl = "XSSJList.aspx";
      //}
    }
    initDSYQXZ();
  }

  protected void InitData(string sID)
  {
    SqlDataReader dr = CBSJ.GetCBSJ(sID);
    if (dr.Read())
    {
      txtYSQL.Text = dr["ysql"].ToString();
      hlYSQL.Value = txtYSQL.Text;
      lbKHID.Text = dr["khid"].ToString();
      lbRQ.Text = dr["rq"].ToString();
      txtBZ.Text = dr["cbbz"].ToString();
      hlBZ.Value = txtBZ.Text;
      lbBYBS.Text = dr["BYBS"].ToString();
      lbSJSYQL.Text = dr["sjyql"].ToString();
    }
    dr.Close();

    lbSYBS.Text = CBSJ.GetSCBS(lbKHID.Text, Convert.ToDateTime(lbRQ.Text));
  }

  //获取所选用户的用气性质
  protected DataSet getDSYQXZ()
  {
    bool flag = false;
    string sSql = "select * from sjlr_bqql where cbsjid=@0";
    SqlParameter[] cmdParams = { new SqlParameter("@0", SqlDbType.VarChar, 10) };
    cmdParams[0].Value = CommonUse.GetID();
    SqlDataReader dr = DbHelperSQL.ExecuteReader(sSql, cmdParams);
    if (dr.Read())
    {
      flag = true;
    }
    dr.Close();
    if (!flag)  //当本期气量表中没数时
    {
      sSql = @"select a.*,d.bqql from JCXX_yqxz a join jcxx_khyqxz b on a.yqxzid=b.yqxzid join sjlr_cbsj c on b.khid=c.khid
            and c.khid=b.khid and c.cbsjid=@0 left join sjlr_bqql d on c.cbsjid=d.cbsjid and  d.yqxzid=a.yqxzid";
    }
    else
    {
      sSql = @"select b.*,a.bqql from sjlr_bqql a left join jcxx_yqxz b on a.yqxzid=b.yqxzid
 left join jcxx_khyqxz c on a.yqxzid=c.yqxzid join sjlr_cbsj d on a.cbsjid=d.cbsjid and c.khid=d.khid and a.cbsjid=@0";
    }
    cmdParams = new SqlParameter[1] { new SqlParameter("@0", SqlDbType.VarChar, 10) };
    cmdParams[0].Value = CommonUse.GetID();
    DataSet ds = DbHelperSQL.Query(sSql, cmdParams);
    return ds;
  }

  protected void initDSYQXZ()
  {
    DataSet ds = getDSYQXZ();
    if (ds.Tables[0].Rows.Count == 1)//当该用户只有一种用气性质时不生成
    {
      //Panel1.Visible = false;
      return;
    }
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
      Panel1.Controls.Add(new LiteralControl("<tr>"));

      Panel1.Controls.Add(new LiteralControl("<td  width=\"25%\" height=20 align=\"right\" bgcolor=\"#E7E7E7\">"));
      Label lb = new Label();
      lb.ID = "l" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
      lb.Text = ds.Tables[0].Rows[i]["yqxz"].ToString();
      Panel1.Controls.Add(lb);
      Panel1.Controls.Add(new LiteralControl("</td>"));

      Panel1.Controls.Add(new LiteralControl("<td  width=75% bgcolor=#FFFFFF>"));
      TextBox textBox = new TextBox();
      textBox.ID = "t" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
      textBox.Text = ds.Tables[0].Rows[i]["bqql"].ToString();
      Panel1.Controls.Add(textBox);
      RequiredFieldValidator rfv = new RequiredFieldValidator();
      rfv.ID = "r" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
      rfv.ErrorMessage = "*";
      rfv.ControlToValidate = "t" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
      Panel1.Controls.Add(rfv);
      RangeValidator rv = new RangeValidator();
      rv.ID = "rv" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
      rv.ErrorMessage = "请输入数字";
      rv.ControlToValidate = "t" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
      rv.MaximumValue = "100000000";
      rv.MinimumValue = "0";
      rv.Type = ValidationDataType.Integer;
      Panel1.Controls.Add(rv);
      Panel1.Controls.Add(new LiteralControl("</td>"));

      Panel1.Controls.Add(new LiteralControl("</tr>"));
    }
  }

  protected void btnConfirm_Click(object sender, EventArgs e)
  {
    //if (isCLYH())
    //{
    //  int jzql = GetJZQL();
    //  int zlql = GetZLQL();
    //  int clql = GetCLQL();
    //  int bcql = 0;
    //  TextBox txt13 = (TextBox)Panel1.FindControl("t13");
    //  if (txt13 != null)
    //  {
    //    bcql += Convert.ToInt32(txt13.Text);
    //  }
    //  TextBox txt15 = (TextBox)Panel1.FindControl("t15");
    //  if (txt15 != null)
    //  {
    //    bcql += Convert.ToInt32(txt15.Text);
    //  }
    //  if (bcql > 0)
    //  {
    //    if ((zlql + clql + bcql) > jzql)
    //    {
    //      lbMessage.Text = "存量气量已超过基准气量，请拆分至增量气量";
    //      return;
    //    }
    //  }
    //}
    Update();
  }

  protected bool CheckCF(DataSet ds)
  {
    double dCFYQXZ = 0;
    string sSJSY = txtYSQL.Text;
    if (ds.Tables[0].Rows.Count == 1)
    {
      //sYQXZ_BQQL = ds.Tables[0].Rows[0]["yqxz"].ToString() + ":" + sSJSY;
      dCFYQXZ = Convert.ToDouble(sSJSY);
    }
    else
    {
      for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
      {
        Label lb = (Label)Panel1.FindControl("l" + ds.Tables[0].Rows[i]["yqxzid"].ToString());
        TextBox txt = (TextBox)Panel1.FindControl("t" + ds.Tables[0].Rows[i]["yqxzid"].ToString());
        //sYQXZ_BQQL = sYQXZ_BQQL + lb.Text + ":" + txt.Text + ",";
        dCFYQXZ = dCFYQXZ + Convert.ToDouble(txt.Text);
      }
      //sYQXZ_BQQL = sYQXZ_BQQL.Trim(new char[] { ',' });
    }
    bool flag = false;
    if (sSJSY.Equals(dCFYQXZ.ToString()))
    {
      flag = true;
    }
    return flag;
  }

  //修改
  protected void Update()
  {
    if (CBSJ.ExistHQID(lbKHID.Text, lbRQ.Text))
    {
      lbMessage.Text = "本次日期之后已有数据，不能修改";
      return;
    }
    string sSYBS = lbSYBS.Text;
    DataSet ds = getDSYQXZ();
    if (ds.Tables[0].Rows.Count == 0)
    {
      lbMessage.Text = "该用户未选择用气性质，请与管理员联系";
      return;
    }
    if (!CheckCF(ds))
    {
      lbMessage.Text = "实际用气量为与拆分后不一致,请重新录入";
      return;
    }
    if ((!lbSJSYQL.Text.Equals(txtYSQL.Text)) && (string.IsNullOrEmpty(txtBZ.Text)))
    {
      lbMessage.Text = "请在备注填写应收气量与实际气量不一致原因";
      return;
    }

    SqlParameter[] cmdParams = null;
    string sSql = "";
    List<CommandInfo> cis = new List<CommandInfo>();
    CommandInfo ci;
    ArrayList arrValue = new ArrayList();
    ArrayList arrLengh = new ArrayList();

    sSql = "update sjlr_cbsj set ysql=@1,cbbz=@2 where cbsjid=@0";
    cmdParams = new SqlParameter[3] { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 10) ,
    new SqlParameter("@2", SqlDbType.VarChar, 200) };
    cmdParams[0].Value = CommonUse.GetID();
    cmdParams[1].Value = txtYSQL.Text;
    cmdParams[2].Value = txtBZ.Text;
    ci = new CommandInfo();
    ci.CommandText = sSql;
    ci.Parameters = cmdParams;
    cis.Add(ci);

    cis.Add(CommonUse.DeleteCI("SJLR_bqql", "cbsjid", CommonUse.GetID()));

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
      TextBox txt = (TextBox)Panel1.FindControl("t" + ds.Tables[0].Rows[i]["yqxzid"].ToString());
      DataTable dtJG = JGGL.GetJG(ds.Tables[0].Rows[i]["yqxzid"].ToString(), lbRQ.Text);
      if (dtJG.Rows.Count == 0)
      {
        lbMessage.Text = "还未录入价格，请与管理员联系";
        return;
      }

      arrValue = new ArrayList();
      arrValue.Add(ds.Tables[0].Rows[i]["yqxzid"].ToString());
      arrValue.Add(CommonUse.GetID());
      arrValue.Add(ds.Tables[0].Rows.Count == 1 ? txtYSQL.Text : txt.Text);
      arrValue.Add("0");
      arrValue.Add(dtJG.Rows[0]["jg"].ToString());
      arrValue.Add(dtJG.Rows[0]["jj"].ToString());
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrLengh = new ArrayList();
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      cis.Add(CommonUse.InsertCI("sjlr_bqql", arrValue, arrLengh));

      sSql = @"update sjlr_bqql set ysjg=ceiling(jg*bqql*100)/100,ysjj=ceiling(jj*bqql*100)/100,
      ysfy=ceiling((jg+jj)*bqql*100)/100,ssql=(-1)*bqql,ssjg=(-1)*ceiling(jg*bqql*100)/100,ssjj=(-1)*ceiling(jj*bqql*100)/100,
      ssfy=(-1)*ceiling((jg+jj)*bqql*100)/100
      where yqxzid=@0 and cbsjid=@1";
      cmdParams = new SqlParameter[2] { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 12) };
      cmdParams[0].Value = ds.Tables[0].Rows[i]["yqxzid"].ToString();
      cmdParams[1].Value = CommonUse.GetID();
      ci = new CommandInfo();
      ci.CommandText = sSql;
      ci.Parameters = cmdParams;
      cis.Add(ci);
    }

    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      //ZHCX.CreateZHCX(lbKHID.Text, lbRQ.Text);
      //ZHCX.UpdateCB(lbKHID.Text, lbRQ.Text, CommonUse.GetID());
      string nr = "";
      if (txtYSQL.Text != hlYSQL.Value)
        nr += "应收气量由" + hlYSQL.Value + "改为" + txtYSQL.Text + "  ";
      if (txtBZ.Text != hlBZ.Value)
        nr += "备注由" + hlBZ.Value + "改为" + txtBZ.Text + "  ";
      if (nr != "")
        rzcz.InsertRZ("修改气量明细:客户编码为" + lbKHID.Text + ",日期为" + lbRQ.Text + nr);
      lbMessage.Text = "保存成功";
    }
  }

  protected void btnBack_Click(object sender, EventArgs e)
  {
    DataSet ds = getDSYQXZ();
    if (!CheckCF(ds))
    {
      List<CommandInfo> cis = new List<CommandInfo>();

      cis.Add(CommonUse.DeleteCI("SJLR_CBSJ", "cbsjid", CommonUse.GetID()));
      cis.Add(CommonUse.DeleteCI("SJLR_bqql", "cbsjid", CommonUse.GetID()));

      if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
      {

      }
    }
    string sMode = Request.QueryString["mode"].ToString();
    if (sMode == "1")
    {
      Server.Transfer("XSSJKHList.aspx");
    }
    else
    {
      Server.Transfer("XSSJList.aspx");
    }
  }

  ////////以下为判断存量指标所用方法
  //是否属于需要比较存量用户
  protected bool isCLYH()
  {
    bool flag = false;
    string sql = "select * from sjlr_cbsj_jzql a,sjlr_cbsj_jznf b where a.nf=b.nf and khid=" + lbKHID.Text;
    SqlDataReader dr = DbHelperSQL.ExecuteReader(sql);
    if (dr.Read())
    {
      flag = true;
    }
    dr.Close();
    return flag;
  }
  //获取基准气量
  protected int GetJZQL()
  {
    int ql = 0;
    string sql = "select * from sjlr_cbsj_jzql a,sjlr_cbsj_jznf b where a.nf=b.nf and khid=" + lbKHID.Text;
    SqlDataReader dr = DbHelperSQL.ExecuteReader(sql);
    if (dr.Read())
    {
      ql = Convert.ToInt32(dr["jzql"].ToString()) + Convert.ToInt32(dr["tzql"].ToString());
    }
    dr.Close();
    return ql;
  }
  //获取增量气量
  protected int GetZLQL()
  {
    int ql = 0;
    string sql = "select * from sjlr_cbsj_jzql a,sjlr_cbsj_jznf b where a.nf=b.nf and khid=" + lbKHID.Text
      + " and a.nf=" + DateTime.Now.Year;
    SqlDataReader dr = DbHelperSQL.ExecuteReader(sql);
    if (dr.Read())
    {
      ql = Convert.ToInt32(dr["jzql"].ToString()) + Convert.ToInt32(dr["tzql"].ToString());
    }
    dr.Close();
    return ql;
  }
  //获取存量气量
  protected int GetCLQL()
  {
    int ql = 0;
    string sql = "select sum(bqql) bqql from sjlr_cbsj a,sjlr_bqql b where a.cbsjid=b.cbsjid and a.khid=" + lbKHID.Text
      + " and (b.yqxzid=13 or b.yqxzid=15)";
    SqlDataReader dr = DbHelperSQL.ExecuteReader(sql);
    if (dr.Read())
    {
      ql = Convert.ToInt32(dr["bqql"].ToString() == "" ? "0" : dr["bqql"].ToString());
    }
    dr.Close();
    return ql;
  }
}